package cn.com.clearbus.service.impl;


import cn.com.clearbus.dao.Userdao;
import cn.com.clearbus.dto.response.ResultBean;
import cn.com.clearbus.entity.CUser;
import cn.com.clearbus.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserServiceImpl implements UserService {

    Logger logger = LoggerFactory.getLogger(getClass());

    //服务发现
    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    Userdao userdao;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;


    @Override
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        System.out.println("services:"+services);
        List<ServiceInstance> instances = discoveryClient.getInstances("CLEARBUS-USER");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }

    @Override
    public CUser getUser(String id) {
        return userdao.getOne(id);
    }

    @Override
    public ResultBean deleteUser(String id) {
        userdao.deleteById(id);
        return new ResultBean();
    }

    @Override
    public ResultBean updateUser(String id) {
        return null;
    }

    @Override
    public CUser saveUser(CUser cUser) {
        //cUser.setId(IDUtil.genId());
        CUser user = userdao.save(cUser);
        logger.info("存储数据为:{}",user);
        return user;
    }



}
